import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import './assets/css/global.css'
// import './mock/index.js'
Vue.config.productionTip = false
//cookie
import Cookies from 'js-cookie'
//定义白名单
const whiteList = ['/','/login'];
router.beforeEach((to,from,next)=>{
  //to   将要进入的路由
  //from  将要离开的路由
  //next   让路由继续执行

  //获取当前打开的选项卡
  // store.commit('getTabs');
  //设置当前激活的选项卡
  // store.commit('setActiveTabs', to.name);
  // 获取token
  let token = Cookies.get('token');
  if(token){
    if(to.path == '/login' || to.path == '/'){
      next({path:'/home'});
    }else{
      let menuList = store.state.menuList;
      if(menuList && menuList.length>0){
        next();
      }else{
        try{
          //延时一下，防止报错
          setTimeout(async function () {
            await store.dispatch("getMenuList",router)
          }, 700);
          next();
        }catch(error){
          console.log(error);
          Cookies.remove('token');
          next({path:'/login'})
        }
      }
    }
    let tabObject = sessionStorage.getItem('tabObject');
    // console.log(tabObject);
    if(tabObject){
        store.commit('initTabList',JSON.parse(tabObject));
    }else{
      store.commit('initTabList',{
          'tabList':[
              {
                  title: '首页',
                  name: '/desktop',
                  // content: 'Tab 1 content'
              }
          ],
          'activeTab':'/desktop'
        });
    }
  }else{
    console.log('1111111');
    if(whiteList.indexOf(to.path) != -1){
      next();
    }else{
      next({path:'/login'})
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
